class Channel
{
    PApplet mom;
    Sequence sequence;
    
    ChannelUI ui;
    int id;
    int channel;
    
    Aggregator localAggregator;
    
    public int numNotes = 8;
    public int localDuration = 0;
    public int probSilence = 5;
    public int[] octaves = {3,4,5};
    
    Channel(PApplet _mom, int _id)
    {
        mom = _mom;
        channel = _id;
        id = _id;
        localAggregator = new Aggregator();
        println("Start Channel: " + channel);
        ui = new ChannelUI(this);
    }
    
    
    void removeSequence()
    {
        println("remove sequence");
    }
    
    void addSequence()
    {
        println("add sequence");
        
        sequence = new Sequence();
        
        int currentDuration = (localDuration == 0) ? mainDuration : localDuration;
        int noteDuration = currentDuration / numNotes;
        
        // declaring some notes
        for(int i=0; i<numNotes; i++)
        {
          
              
            int[] notes = {0, 2, 4, 5, 7, 9, 11};
            int octava = octaves[ int(random(octaves.length)) ];
            int noteslength = notes.length;
            int rnd = int(random(noteslength));
            int notedef = notes[ rnd ] + 12*octava;    
            int pitch = int(random(127));
            int velocity = 127;
            
            boolean isMute = (int(random(0,probSilence))==0) ? true : false;
          
            //Note(float freq, float dur, float pan, float vel, float sust, boolean r false, String comm, int ch); 
            
            
            Note note = new Note(noteDuration, false);
            note.setChannel(channel);
            note.setVelocity(velocity);
            note.setFrequency(notedef);
            
            if(isMute)
            {
              println("mute");
                note.setFrequency(-1);
            }
            sequence.addNote(note);
        }
      
        //mainAggregator.resetAll();
        //mainAggregator = new Aggregator();
        mainAggregator.addSequence(sequence);
        
        
       updateTactu5();
    }    
    
    void updateTactu5()
    {
        if(tactu5 == null)
        {
             println("   INIT TACTUS");
             tactu5 = new Tactu5(mom,mainAggregator.getScore(),true);
             tactu5.start(); 
        }else{
             tactu5.updateSequencer(mainAggregator.getScore());
        }
      
    }
}
